package com.lbssoft.gpsbd.dao;

import java.util.List;
import java.util.Map;

import com.lbssoft.gpsbd.model.GroupDeviceTree;
import com.lbssoft.gpsbd.model.vo.DeviceVo;
import com.lbssoft.gpsbd.model.vo.Move;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.lbssoft.gpsbd.dao.sql.DeviceSql;
import com.lbssoft.gpsbd.model.Device;
import com.lbssoft.gpsbd.model.DevicePermission;

@Mapper
public interface DeviceMapper {
	/**
	 * 添加设备
	 * @param device
	 */
	@InsertProvider(type = DeviceSql.class, method = "insert")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void addDevice(Device device);
	
	/**
	 * 增加用户与设备关系
	 * @param uid
	 * @param did
	 */
	@Insert("INSERT INTO lbs_user_device(userid,deviceid) VALUE(#{uid},#{did})")
	void addUserDevice(@Param("uid") long uid,@Param("did") long did);

	/**
	 * 根据设备号查询设备信息
	 * @param imei
	 * @return
	 */
	@Select("SELECT * FROM lbs_devices where imei = #{imei}")
	Device getDeviceByImei(@Param("imei") String imei);
	
	/**
	 * 根据设备ID查询设备信息
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM lbs_devices where id = #{id}")
	Device getDeviceById(@Param("id") long id);
	
	/**
	 * 更新设备最后位置点
	 * @param positionId
	 * @param id
	 */
	@Update("UPDATE lbs_devices SET positionId =#{positionId},lastupdate=now() WHERE id = #{id}")
	void updateLastPosition(@Param("positionId") long positionId,@Param("id") long id);
	
	/**
	 * 更新设备状态
	 * @param status
	 * @param id
	 */
	@Update("UPDATE lbs_devices SET `status` = #{status} where id = #{id}")
	void updateStatus(@Param("status") int status,@Param("id") long id);
	
	/**
	 * 修改设备信息
	 * @param device
	 */
	@UpdateProvider(type = DeviceSql.class, method = "update")
	void updateDevice(Device device);
	
	/**
	 * 删除设备
	 * @param id
	 */
	@Delete("DELETE FROM lbs_devices where id = #{id}")
	void delDevice(@Param("id") long id);
	
	/**
	 * 根据用户id获取用户下所有设备
	 * @param uid
	 * @return
	 */
	@Select("SELECT a.id,a.name,a.imei,a.typeid,a.groupId,a.sim,ifnull(a.`status`,-1) as status,a.lastupdate,a.speedLimit,a.time FROM lbs_devices a LEFT JOIN lbs_user_device b ON a.id = b.deviceid where b.userid = #{uid} ORDER BY a.`status` DESC")
	List<Device> getDevicesByUid(@Param("uid") long uid);
	
	
	/**
	 * 获取设备用户关系
	 * @return
	 */
	@Select("SELECT userId, deviceId FROM lbs_user_device")
	List<DevicePermission> getDevicePermissions();
	
	/**
	 * 获取所有设备
	 * @return
	 */
	@Select("SELECT * FROM lbs_devices order by id desc")
	List<Device> getAllDevices();
	
	/**
	 * 获取设备类型字典
	 * @return
	 */
	@Select("SELECT id,name FROM lbs_device_type")
	List<Map<Long,String>> getTypes();

	/**
	 * 更新设备状态属性
	 * @param id
	 * @param state
     */
//	@Update("UPDATE lbs_devices SET state = #{state} WHERE id = #{id}")
//	void upateState(@Param("id") long id,@Param("state") String state);

	/**
	 * 根据用户id获取分组 设备的属性结构
	 * @param userId
	 * @return
     */
	@Select("SELECT a.id, a.name, IFNULL(a.groupId, 0) AS parentId, 'group' AS type,1 as status FROM lbs_user_group t LEFT JOIN lbs_groups a ON a.id = t.groupId WHERE t.userId = #{userId} UNION SELECT c.id, c. NAME, IFNULL(c.groupId, 0) AS parentId, 'device' AS type,c.`status` FROM lbs_user_device b LEFT JOIN lbs_devices c ON b.deviceid = c.id WHERE b.userid = #{userId} ORDER BY type desc,`status` desc")
	List<GroupDeviceTree> getTree(@Param("userId") long userId);

	/**
	 * 更新里程
	 * @param distance
	 * @param id
     */
//	@Update("UPDATE lbs_devices SET  todayTrip=todayTrip+#{distance},trip=trip+#{distance} WHERE id = #{id}")
//	void updateTrip(@Param("distance") double distance,@Param("id") long id);

	/**
	 * 修改设备分组
	 * @param move
     */
	@Update("UPDATE lbs_devices SET groupId=#{groupId} where id = #{id}")
	void move(Move move);

	/**
	 * 管理员获取设备列表
	 * @return
     */
	@Select("SELECT * FROM ( SELECT t.*, c. NAME AS typeName, b.account FROM lbs_devices t LEFT JOIN lbs_user_device a ON a.deviceid = t.id LEFT JOIN lbs_users b ON b.id = a.userid LEFT JOIN lbs_device_type c ON c.id = t.typeid ) AS temp ${where}")
	List<DeviceVo> getDevices(@Param("where") String where);
	
}
